package com.first.wchat;

import java.util.ArrayList;

import com.first.wchat.data.Constant;
import com.first.wchat.data.DeviceAndGroupListener;
import com.first.wchat.data.MSGDataManager;
import com.first.wchat.util.ScanUtil;
import com.wchat.network.Actions;
import com.wchat.network.Configs;
import com.wchat.network.Controller;
import com.wchat.network.device.Device;
import com.wchat.network.device.DeviceContext;
import com.wchat.network.device.SendTarget;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.wchat.network.message.Message;

public class WChatActivity extends Activity implements DeviceAndGroupListener{

	EditText mEditor;
	Button mSender;
	ListView mUserList;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

		initView();
		MSGDataManager.getInstance().addListener(this);
    }
    
    
    public void onStart()
    {
    	super.onStart();
    }
    
    public void onDestroy()
    {
    	MSGDataManager.getInstance().removeListener(this);
    	super.onDestroy();
    }
    
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	menu.clear();
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
    	
    }
    public boolean onOptionsItemSelected(MenuItem item){
    	
    	if(item.getItemId() == R.id.myip)
    	{
    		TextView ip = new TextView(this);
    		ip.setText(ScanUtil.getLocalIP());
    		new AlertDialog
    		.Builder(this)
    		.setView(ip)
    		.setPositiveButton("ok", null)
    		.create().show();
    		
    	}
    	else if(item.getItemId() == R.id.scan)
    	{
    		
    	}
    	
    	return true;
    }
    
    private void initView()
    {
    	mUserList = (ListView)findViewById(R.id.devices);
    	
    	if(mUserList == null) return;
    	DeviceAdapter adapter = new DeviceAdapter(MSGDataManager.getInstance().getDevices());
    	
    	mUserList.setAdapter(adapter);
    	
    	mUserList.setOnItemClickListener(new MyOnItemClickListener());
    }
    
    private class DeviceAdapter extends BaseAdapter {

		ArrayList<Device> devices;

		public DeviceAdapter(ArrayList<Device> devices) {
			super();
			this.devices = devices;
		}

		@Override
		public int getCount() {
			return devices.size();
		}

		@Override
		public Object getItem(int position) {
			return devices.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View contentView, ViewGroup parent) {
			TextView tv = (TextView) View.inflate(WChatActivity.this, R.layout.testtv, null);
			Device device = (Device) getItem(position);
			tv.setText("[" + device.getAddress() + "] " + device.getName());
			return tv;
		}

	}
    
    
    class MyOnItemClickListener implements OnItemClickListener
    {

		@Override
		public void onItemClick(AdapterView<?> listView, View view, int p1,
				long p2) {
			Device d = (Device)listView.getItemAtPosition(p1);
			Intent intent = new Intent(WChatActivity.this, ChatActivity.class);
			intent.putExtra("Target",d);
			WChatActivity.this.startActivity(intent);
			
		}
    	
    }


	@Override
	public void newMessage(SendTarget target, Object o) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void Join(SendTarget target) {
		initView();
		
	}


	@Override
	public void leave(SendTarget target) {
		initView();
	}


	@Override
	public void newMessage(SendTarget group, SendTarget target, Object o) {
		// TODO Auto-generated method stub
		
	}

}